﻿Imports System.IO

Public Class SuperOpenFile

    Public Property NameElement As String
    Public Property FileName As String
    Public Property FileNameComplete As String
    Public Property NamePath As String
    Public Property Filter As String
    Public Property DimMaxFileName As Integer = 9000

    Public Sub SetFile(ByVal inFileNameComplete As String)
        Try
            FileNameComplete = inFileNameComplete
            Dim oFileInfo As New FileInfo(inFileNameComplete)
            NamePath = oFileInfo.DirectoryName

            txtFile.Text = FileNameComplete
        Catch ex As Exception
            Base.ShowError.Show("Set file", Me.Name, "SetFile", ex)
        End Try
    End Sub

    Private Sub OpenFile()
        Try
            Dim oOpenFileDialog As New System.Windows.Forms.OpenFileDialog

            With oOpenFileDialog
                .CheckFileExists = True
                .CheckPathExists = True
                .Filter = Filter()
                .InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
                .Multiselect = False
                .Title = "Select " & NameElement
            End With

            If oOpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                If oOpenFileDialog.SafeFileName.Length > DimMaxFileName Then
                    MsgBox("File name lenght must be less than " & DimMaxFileName & " characters", vbExclamation, "Select file")
                Else
                    FileNameComplete = oOpenFileDialog.FileName
                    FileName = oOpenFileDialog.SafeFileName
                    Dim oFileInfo As New FileInfo(FileNameComplete)
                    NamePath = oFileInfo.DirectoryName

                    txtFile.Text = FileNameComplete
                End If
            End If

            oOpenFileDialog = Nothing
        Catch ex As Exception
            Base.ShowError.Show("Open file", Me.Name, "cmdOpenFile_Click", ex)
        End Try
    End Sub

    Private Sub SuperOpenFile_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        txtFile.Width = Width - 35
        cmdOpenFile.Left = txtFile.Width + 7
    End Sub

    Private Sub cmdOpenFile_OnClickOrPress(ByRef sender As SuperSmallButton) Handles cmdOpenFile.OnClickOrPress
        Call OpenFile()
    End Sub

End Class
